/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.storage.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.storage.entity.Content;
import com.jeesite.modules.storage.service.ContentService;

/**
 * 内容Controller
 */
@Controller(value = "webcontent")
@RequestMapping(value = "${adminPath}/storage/content")
public class ContentController extends BaseController {

	@Autowired
	private ContentService contentService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Content get(String id, boolean isNewRecord) {
		return contentService.get(id, isNewRecord);
	}

	/**
	 * 查看编辑表单
	 */
	@RequestMapping(value = "form")
	public String form(Content content, Model model) {
		model.addAttribute("content", content);
		return "modules/storage/content/form";
	}

	/**
	 * 保存内容
	 */
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Content content) {
		try{
			contentService.save(content);
			return renderResult(Global.TRUE, text("保存内容成功！"));
		}catch (Exception ex){
			return renderResult(Global.FALSE, text(ex.getMessage()));
		}
	}

}